// pages/my/mycoupon/mycoupon.js
import { axios } from '../../../utils/ajax.js' 
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navindex: 0,
    data:'',
    //平台券
    ptcoupon:'',
    //专区券
    zqcoupon:'',
    maskstate:false,
    couponboxshow: false,
    iPhoneX: false,
    //专区列表信息
    specials:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //x适配
    console.log(app.globalData.model)
    if (app.globalData.model.substr(0, 8) == 'iPhone X') {
      this.setData({
        iPhoneX: true
      })
    }



    let { userid, token } = wx.getStorageSync('userinfo')
    console.log(userid, token )
    axios({
      url: '/coupon/couponList',
      data: {
        userid: userid, 
        token: token  
      }
    }).then(res => {
      console.log(res)
      this.setData({
        data:res.data.data
      })
      this.setData({
        ptcoupon: res.data.data?res.data.data.filter(x => x.type == 1):'',
        zqcoupon: res.data.data ? res.data.data.filter(x => x.type == 2) : '',
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //导航切换
  changenav (e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      navindex: index
    })
  },
  //点击优惠券跳转
  clickcoupon (e) {
    let specialid = e.currentTarget.dataset.specialid
    let type = e.currentTarget.dataset.type
    console.log(specialid, type)
    //专区优惠券
    if (type == 2){
      wx.navigateTo({
        url: `../../home/zone/zone?linkId=${specialid}`,
      })
    //平台优惠券 展开优惠券弹窗
    }else{
      let index = e.currentTarget.dataset.index
      console.log(index)
      console.log(this.data.ptcoupon[index].specials)
      if (this.data.ptcoupon[index].specials){
        this.setData({
          specials: this.data.ptcoupon[index].specials
        })
        this.setData({
          couponboxshow: true,
          maskstate: true
        })
      }
    }
  },
  //碳层关闭
  thisclose () {
    this.setData({
      couponboxshow: false,
      maskstate: false
    })
  },
  //选择这个专区券跳转
  gothisspecial (e) {
    let specialid = e.currentTarget.dataset.specialid
    console.log(specialid)
    wx.navigateTo({
      url: `../../home/zone/zone?linkId=${specialid}`,
    })
    this.thisclose()
  }
})